<template>
  <div class='container-articles'>
    <el-card>
      <el-row>
        <el-col :span="18">
          <el-form ref="artivleForm" :model="formDate"  label-width="60px" inline  size="mini">
            <el-form-item label="关键词">
              <el-input v-model="formDate.keyword" placeholder="根据文章标题搜索"></el-input>
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="formDate.state" placeholder="请选择">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button size="small" @click="empty" >清除</el-button>
              <el-button size="small" type="primary" @click="search">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-button style="float: right" type="success" size="small" icon="el-icon-edit" @click="showDialog = true">新增技巧</el-button>
        <el-col :span="6"></el-col>
      </el-row>
      <el-alert
        class="article-alert"
        :title="`数据一共${total}条`"
        type="info"
        :closable="false"
        show-icon>
      </el-alert>
      <!-- 表格 -->
      <el-table :data="tableData">
        <el-table-column
          type="index"
          label="序号">
        </el-table-column>
        <el-table-column label="文章标题">
          <template slot-scope="scope">
            <div class="cell">
              {{ scope.row.title }}
              <a v-if="scope.row.videoURL" href="#" class="el-icon-film" @click="videoShow = true"></a>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="visits" label="阅读数"></el-table-column>
        <el-table-column prop="username" label="录入人"></el-table-column>
        <el-table-column label="录入时间">
          <template slot-scope="scope">
            {{ scope.row.createTime | parseTimeByString }}
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            {{ scope.row.state === 1 ? '已启用' : '已禁用' }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="preview(scope.row)">预览</el-button>
            <el-button type="text" @click="changeState(scope.row.id, scope.row.state)">
              {{ scope.row.state === 1 ? '禁用' : '启用' }}
            </el-button>
            <el-button type="text" :disabled="scope.row.state === 0" @click="updateArticle(scope.row.id)">修改</el-button>
            <el-button type="text" :disabled="scope.row.state === 0" @click="delArticle(scope.row.id)">删除</el-button>
          </template>         
        </el-table-column>
      </el-table>
      <!-- 底部分页栏 -->
      <div class="block">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="10"
          layout="prev, pager, next, sizes, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>
    <!-- 新增弹层 -->
    <articles-add ref="addArticles" :show-dialog.sync="showDialog" @addArticle="getlist()" />
    <!-- 预览文章 -->
    <articles-preview :preview-data="previewData" :show-preview.sync="showPreview" />
    <div v-if="videoShow" class="video-preview">
      <div class="close" @click="videoShow = false">
        <span class="el-icon-close"></span>
      </div>
      <div class="box">
        <video :src="previewData.videoURL"></video>
      </div>
    </div>
  </div>
</template>

<script>
import { list, remove, changeState } from '@/api/hmmm/articles.js'
import ArticlesAdd from '../components/articles-add'
import ArticlesPreview from '../components/articles-preview'
export default {
  components: {
    ArticlesAdd,
    ArticlesPreview
  },
  data () {
    return {
      formDate: {
        keyword: null, // 关键词
        state: null, // 状态null
        page: 1, // 页数
        pagesize: 10 // 页数据数
      },
      total: '', // 数据总数
      tableData: {}, //表格数据
      showDialog: false,
      showPreview: false,
      articlesId: null,
      previewData: {}, //记录点击节点文章数据
      videoShow: false // 视频播放显示
    }
  },
  created () {
    this.getlist()
  },
  methods: {
    async getlist () {
      // console.log(await list(this.formDate))
      const { data } = await list(this.formDate)
      this.tableData = data.items
      this.total = data.counts
    },
    handleSizeChange (val) { // 当每页条数发生改变
      this.formDate.pagesize = val
      this.getlist()
    },
    handleCurrentChange (val) { // 当前页数发生改变
      this.formDate.page = val
      this.getlist()
    },
    delArticle (id) { // 删除文章
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(async () => {
        await remove({id: id})
        this.$message.success('删除成功')
        this.getlist()
      })
    },
    async changeState (id, state) { // 修改状态
      if (state === 0) {
        state = 1
      } else {
        state = 0
      }
      await changeState({ id: id, state: state })
      this.getlist()
    },
    async updateArticle (id) { // 修改文章
      this.articlesId = id
      await this.$refs.addArticles.detail(id)
      this.showDialog = true
    },
    empty () { // 清空表单
      this.formDate = {
        keyword: null,
        state: null,
        page: 1,
        pagesize: 10 
      }
      this.getlist()
    },
    search () {
      this.getlist()
    },
    preview (data) {
      this.previewData = data
      this.showPreview = true
    }
  }
}
</script>

<style scoped lang='scss'>
.container-articles {
  padding: 10px 15px;
  .article-alert {
    margin-bottom: 10px;
  }
  .cell {
    box-sizing: border-box;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: normal;
    word-break: break-all;
    line-height: 23px;
    padding-right: 10px;
    a {
      color: #00f;
      font-size: 18px;
    }
  }
  .video-preview {
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,.3);
    overflow: hidden;
    z-index: 9999;
    .close {
      width: 50px;
      height: 50px;
      position: absolute;
      top: 30px;
      left: 50%;
      -webkit-transform: translate(-50%);
      transform: translate(-50%);
      background: rgba(0,0,0,.4);
      -webkit-box-shadow: 0 0 5px rgb(0 0 0 / 40%);
      box-shadow: 0 0 5px rgb(0 0 0 / 40%);
      border-radius: 50%;
      text-align: center;
      line-height: 50px;
      color: #fff;
      font-size: 20px;
    }
    .box {
      width: 800px;
      height: 600px;
      position: absolute;
      left: 50%;
      top: 50%;
      -webkit-transform: translate(-50%,-50%);
      transform: translate(-50%,-50%);
      video {
        width: 100%;
        height: 100%;
      }
    }
  }
}
</style>
